package com.liveinstars.common.lock;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * Created by guoyanjiang on 2018/5/8.
 */
@Aspect
@Component
public class OptimisticLockAspect {

    //更新操作返回1-成功更新 -1-表示逻辑上不能进行更新了，主动退出
    @Around("@annotation(com.liveinstars.common.lock.OptimisticLock)")
    public Object retry(ProceedingJoinPoint joinPoint) throws Throwable {
        int result = 0;
        do {
            //0-未更新，再次尝试
            result = (int) joinPoint.proceed();
        } while (result == 0);
        return result;
    }
}
